package com.easysoft.framework.action;

import com.easysoft.commons.utils.StringUtils;
import com.easysoft.framework.annotation.logs.SystemControllerLog;
import com.easysoft.framework.beans.Page;
import com.easysoft.framework.copyright.model.CopyrightQuery;
import com.easysoft.framework.copyright.model.CopyrightView;
import com.easysoft.framework.copyright.model.SysCopyright;
import com.easysoft.framework.copyright.service.CopyrightService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by try on 2017/9/12.
 */

@RestController
@RequestMapping(value = SystemCopyrightAction.BASE_VIEW)
public class SystemCopyrightAction {

    public static final String BASE_VIEW = "/copyright";
    private static final Logger logger = LoggerFactory.getLogger(SystemCopyrightAction.class);

    @Autowired
    @Qualifier("service.CopyrightService")
    private CopyrightService copyrightService;

    @RequestMapping(value = "/listCopyright", method = RequestMethod.GET)
    public ModelAndView listCopyright(Page page, CopyrightQuery query, HttpSession session) {
        logger.info("Call SystemCopyrightAction.listCopyright({},{})", page.toString(), query.toString());
        List<CopyrightView> listCopyright = copyrightService.selectCopyrightByPage(page, query);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/listCopyright");
        mv.addObject("page", page);
        mv.addObject("query", query);
        mv.addObject("listCopyright", listCopyright);
        return mv;
    }

    @RequestMapping(value = "/editCopyright", method = RequestMethod.GET)
    public ModelAndView editCopyright(String action, @RequestParam(required = false) String id) {
        logger.info("Call SystemCopyrightAction.editCopyright({},{})", action, id);
        ModelAndView mv = new ModelAndView(BASE_VIEW + "/editCopyright");
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(id)) {
                return null;
            }
            return mv;
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(id)) {
                return null;
            }
            SysCopyright entity = copyrightService.selectByPrimaryKey(id);
            if (entity == null) {
                return null;
            }
            mv.addObject("entity", entity);
            return mv;
        }
        return null;
    }

    @RequestMapping(value = "/saveCopyright", method = RequestMethod.POST)
    public int saveCopyright(String action, SysCopyright copyright, HttpSession session) {
        logger.info("Call SystemCopyrightAction.saveCopyright({},{})", action, copyright.toString());
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("add")) {
            // 添加
            if (StringUtils.isNotNullAndBlank(copyright.getId())) {
                return 0;
            }
            return copyrightService.addSysCopyright(copyright);
        }
        if (StringUtils.isNotNullAndBlank(action) && action.toLowerCase().equals("update")) {
            // 修改
            if (StringUtils.isNullOrBlank(copyright.getId())) {
                return 0;
            }
            return copyrightService.updateSysCopyright(copyright);
        }
        return 0;
    }




    @RequestMapping(value = "/updateSysCopyright", method = RequestMethod.POST)
    public int updateSysCopyright(SysCopyright copyright, HttpSession session) {
        logger.info("Call SystemCopyrightAction.updateSysCopyright({})", copyright.toString());
        return copyrightService.updateSysCopyright(copyright);
    }

    @RequestMapping(value = "/deleteSysCopyright", method = RequestMethod.POST)
    public int deleteSysCopyright(String id) {
        logger.info("Call SystemCopyrightAction.deleteSysCopyright({})", id);
        return copyrightService.deleteSysCopyright(id);
    }
}
